/**
 * 预算日记账详情入口
 * 因为现在日记账详情的编辑和提交后页面是两个不同的文件（头大）
 * 为了统一处理路由并根据单据当前状态渲染不同页面
 * 所以增加了这个入口文件
 */
import React, { Component } from "react";
import { Spin } from "antd";
import BudgetJournalDetail from "./budget-journal-detail";
import BudgetJournalDetailSubmit from "./budget-journal-detail-submit";
import budgetJournalService from "./budget-journal.service";

class BudgetJournalDetailEntrance extends Component {
  state = {
    loading: false,
    info: {},
    urlParams: {},
  };

  componentDidMount() {
    this.setUrlParams();
    this.getInfo();
  }

  setUrlParams = () => {
    const { pathname, query } = this.props.location || {};
    const urlParams = query || {};
    if (pathname && pathname.indexOf("approve-budget-journal") > -1) {
      urlParams.entry = "approve";
      urlParams.status = "approving";
    }
    this.setState({ urlParams });
  };

  getInfo = () => {
    const { entityOID } = this.props.params;
    this.setState({ loading: true });
    budgetJournalService
      .getJournalHeaderInfo(entityOID)
      .then((res) => this.setState({ info: res.data }))
      .finally(() => this.setState({ loading: false }));
  };

  renderDetail = () => {
    const { info, urlParams } = this.state;
    const props = { ...this.props.params, ...urlParams };
    // 审批列表或url跳转进入的页面不可编辑
    if (
      ["NEW", "SUBMIT_RETURN", "APPROVAL_REJECT", "AUDIT_REJECT"].includes(info.status) &&
      props.entry !== "approve"
    ) {
      return <BudgetJournalDetail {...props} />;
    }
    return <BudgetJournalDetailSubmit {...props} close={this.props.close} />;
  };

  render() {
    const { loading, info } = this.state;
    return loading || !info.id ? <Spin /> : this.renderDetail();
  }
}

export default BudgetJournalDetailEntrance;
